import traceback
from selenium.webdriver.support.ui import WebDriverWait
import time


class Base():

    # 封装driver方法
    def __init__(self, driver):
        self.driver = driver

    # 封装打开浏览器的操作
    def open_url(self, url):
        self.driver.get(url)

    # 封装查找页面元素的方法
    def find_element(self, loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            sj = time.strftime('%Y%m%d%H%M')
            self.driver.save_screenshot(f'{sj}base.png')
            print(traceback.format_exc())

    # 封装点击页面元素的方法
    def click(self, loc):
        self.find_element(loc).click()

    # 封装输入方法
    def send_keys(self, loc, text):
        self.find_element(loc).send_keys(text)

    def iframe_kj(self, loc):
        f = self.find_element(loc)
        self.driver.switch_to.frame(f)
